## 子弹基类
## 基础的移动速度，生命值（碰撞次数），已攻击对象等

class_name bullet_base extends Node2D

####### 属性 S  ###########
# 和穿透次数有关
#var hp:int = 0; 
# 攻击延迟
var delay_count_down:float = 0;
#var delay:float = 0;
var radius:float = 10;
var cur_radius:float = 0;
#攻击力
#var attack:int = 0;

var bullet_data:BULLET_DATA;
var on_pool_obj_done:Callable;

####### 属性 E  ###########
@export var speed : float = 10.0
@export var hurtbox : HurtBox

var hit_effects


var skill_eff :AnimatedSprite2D = preload("res://scenes/effect/effect_base.tscn").instantiate();

func _enter_tree():
	if not hurtbox:
		return
	
	hurtbox.on_hit.connect(_on_hit)
	hurtbox.monitoring = false;
	# skill_eff.animation_finished.connect(self.on_effect_finished);
	# skill_eff.connect("animation_finished", on_effect_finished)
	#skill_eff.animation_finished.connect(on_effect_finished)
	hit_effects = self.get_node("Effect")
	hit_effects.animation_finished.connect(on_effect_finished)

func _exit_tree():
	if not hurtbox:
		return
	
	hurtbox.on_hit.disconnect(_on_hit)
	#skill_eff.animation_finished.disconnect(on_effect_finished);
	hit_effects.animation_finished.disconnect(on_effect_finished)

func init(data:BULLET_DATA):
	self.bullet_data = data;

	delay_count_down = data.delay;

func _physics_process(delta):
	if bullet_data.delay > 0 and delay_count_down > 0:
		cur_radius = radius * ( 1 - delay_count_down / self.bullet_data.delay);
		queue_redraw();
		delay_count_down -= delta*1000;

		if delay_count_down <= 0 :
			hurtbox.set_monitoring(true);

		
			hit_effects.sprite_frames = load("res://game_resources/skill_effect/skill001.tres");
			hit_effects.play();
			#self.add_child(skill_eff);
	
	# if delay <= 0 && false == hurtbox.on_hit.is_connected(_on_hit):
	# 	hurtbox.on_hit.connect(_on_hit)
func _draw():
	draw_circle(Vector2(0,0), cur_radius, Color(0, 1, 0));
	# draw_circle(Vector2(0,0), radius, Color(0, 1, 0));
	draw_arc(Vector2(0,0), radius, 0, TAU, 20, Color(0, 1, 0));

var pool_dead:bool = false;
func is_pool_dead():
	return pool_dead;
func set_pool_dead(value:bool):
	pool_dead = value


### 监听Hitbox的on_hit信号
func _on_hit():
	print("bullet_base hit target")
	
	# for effect in hit_effects:
	# 	if effect:
	# 		effect.trigger(self)

func on_effect_finished():
	print("on_effect_finished")
	if on_pool_obj_done:
		on_pool_obj_done.call(self);
	# self.hide();
